package com.cxyzy.glidedemo

import android.graphics.PointF
import android.os.Bundle
import android.widget.ImageView
import android.widget.LinearLayout
import android.widget.TextView
import androidx.appcompat.app.AppCompatActivity
import com.bumptech.glide.Glide
import jp.co.cyberagent.android.gpuimage.filter.GPUImageSoftLightBlendFilter
import jp.wasabeef.glide.transformations.BlurTransformation
import jp.wasabeef.glide.transformations.ColorFilterTransformation
import jp.wasabeef.glide.transformations.GrayscaleTransformation
import jp.wasabeef.glide.transformations.MaskTransformation
import jp.wasabeef.glide.transformations.gpu.*
import kotlinx.android.synthetic.main.activity_filter.*


/**
 * 滤镜
 */
class FilterActivity : AppCompatActivity() {

    private val url = "https://c-ssl.duitang.com/uploads/item/201706/05/20170605121955_mkz2y.thumb.700_0.jpeg"

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_filter)
        loadImage().into(getView("正常"))
        loadImage().transform(GrayscaleTransformation())
                .into(getView("黑白"))
        loadImage()
                .transform(GPUFilterTransformation(GPUImageSoftLightBlendFilter()))
                .into(getView("暖色"))
        loadImage()
                .transform(ColorFilterTransformation(0x7900CCCC))
                .into(getView("颜色过滤"))
        loadImage()
                .transform(BlurTransformation(8, 2))
                .into(getView("模糊"))
        loadImage()
                .transform(MaskTransformation(R.mipmap.ic_launcher))
                .into(getView("图片遮罩"))

        //如下都是GPU过滤（需要依赖GPUImage库）
        loadImage().transform(ToonFilterTransformation(0.2f, 10f))
                .into(getView("卡通滤波器"))
        loadImage().transform(SepiaFilterTransformation(1f))
                .into(getView("乌墨色滤波器"))
        loadImage().transform(ContrastFilterTransformation(3f))
                .into(getView("对比度滤波器"))
        loadImage().transform(InvertFilterTransformation())
                .into(getView("反转滤波器"))
        loadImage().transform(PixelationFilterTransformation(20f))
                .into(getView("像素化滤波器"))
        loadImage().transform(SketchFilterTransformation())
                .into(getView("素描滤波器"))
        loadImage().transform(SwirlFilterTransformation(1.0F, 0.4F, PointF(0.5F, 0.5F)))
                .into(getView("旋转滤波器"))
        loadImage().transform(BrightnessFilterTransformation(0.5f))
                .into(getView("亮度滤波器"))
        loadImage().transform(KuwaharaFilterTransformation(10))
                .into(getView("Kuwahara滤波器"))
        loadImage().transform(VignetteFilterTransformation(PointF(0.5F, 0.5F), floatArrayOf(0.0f, 0.0f, 0.0f), 0.0F, 0.5F))
                .into(getView("装饰图滤波器"))
    }

    private fun getView(text: String): ImageView {
        var linearLayout = LinearLayout(this)
        linearLayout.orientation = LinearLayout.HORIZONTAL
        val textView = TextView(this)
        textView.text = text
        val imageView = ImageView(this)

        val layoutParams = LinearLayout.LayoutParams(800, 800)
        layoutParams.bottomMargin = 10

        linearLayout.addView(textView)
        linearLayout.addView(imageView, layoutParams)
        rootView.addView(linearLayout)
        return imageView
    }

    private fun loadImage() = Glide.with(this).load(url)

}
